//
// GPSMapEdit
// (c) Konstantin Galichsky (kg@geopainting.com), 2005-2007
//
// COM object model: the path object.
//

# include "StdAfx.h"
# include "COMPath.h"
# include "Paths.h"
# include "Map.h"
# include "Wrappers.h"

STDMETHODIMP CComPath::Delete () {
	DeletePath (m_path);
	return S_OK;
}

STDMETHODIMP CComPath::get_Color (long * _ret) {
	* _ret = m_path.col;
	return S_OK;
}

STDMETHODIMP CComPath::put_Color (long _lVal) {
	m_path.col = _lVal;
	return S_OK;
}

STDMETHODIMP CComPath::get_HighlightedColor (long * _ret) {
	* _ret = m_path.colHS;
	return S_OK;
}

STDMETHODIMP CComPath::put_HighlightedColor (long _lVal) {
	m_path.colHS = _lVal;
	return S_OK;
}

STDMETHODIMP CComPath::get_Width (long * _ret) {
	* _ret = m_path.cWidth;
	return S_OK;
}

STDMETHODIMP CComPath::put_Width (long _lVal) {
	m_path.cWidth = _lVal;
	return S_OK;
}

STDMETHODIMP CComPath::get_HighlightedSegmentFrom (long * _ret) {
	* _ret = m_path.cHSFrom;
	return S_OK;
}

STDMETHODIMP CComPath::get_HighlightedSegmentTo (long * _ret) {
	* _ret = m_path.cHSTo;
	return S_OK;
}

STDMETHODIMP CComPath::SetHighlightedSegment (long _lFrom, long _lTo) {
	m_path.cHSFrom = _lFrom;
	m_path.cHSTo   = _lTo;
	return S_OK;
}

STDMETHODIMP CComPath::get_Points (long * _ret) {
	* _ret = m_path.points.size ();
	return S_OK;
}

STDMETHODIMP CComPath::AppendPoint (double _Latitude, double _Longitude) {
	m_path.points.push_back (point_t (_Longitude, _Latitude));
	return S_OK;
}

STDMETHODIMP CComPath::AppendPoints (const VARIANT * _pvPoints) {
	if ((_pvPoints->vt & VT_R8) == 0 || (_pvPoints->vt & VT_ARRAY) == 0)
		return E_INVALIDARG;

	SAFEARRAY * const pPoints = _pvPoints->vt & VT_BYREF ? * _pvPoints->pparray : _pvPoints->parray;
	if (pPoints->cDims != 1)
		return E_INVALIDARG;
	if (pPoints->cbElements != sizeof (double))
		return E_INVALIDARG;
	const size_t cElements = pPoints->rgsabound [0].cElements;
	if ((cElements & 0x1) != 0)
		return E_INVALIDARG;

	const size_t cSize0 = m_path.points.size ();
	m_path.points.resize (cSize0 + cElements/2);

	double * pArray = NULL;
	::SafeArrayAccessData (pPoints, reinterpret_cast<void **> (& pArray));
	for (size_t c = 0; c < cElements/2; ++ c) {
		m_path.points [cSize0 + c].x = pArray [c*2 + 1];
		m_path.points [cSize0 + c].y = pArray [c*2];
	}
	::SafeArrayUnaccessData (pPoints);

	return S_OK;
}

STDMETHODIMP CComPath::DeleteAllPoints () {
	m_path.points.clear ();
	return S_OK;
}

STDMETHODIMP CComPath::get_Hidden (VARIANT_BOOL * _ret) {
	* _ret = m_path.bHidden ? VARIANT_TRUE : VARIANT_FALSE;
	return S_OK;
}

STDMETHODIMP CComPath::put_Hidden (VARIANT_BOOL _Val) {
	m_path.bHidden = _Val != VARIANT_FALSE;
	return S_OK;
}

STDMETHODIMP CComPath::get_Tag (VARIANT * _ret) {
	return ::VariantCopy (_ret, & m_path.vTag);
}

STDMETHODIMP CComPath::put_Tag (const VARIANT * _pVal) {
	return m_path.vTag.Copy (_pVal);
}
